/**
 * @ Author: luoqi
 * @ Create Time: 2025-06-29 20:00
 * @ Modified by: luoqi
 * @ Modified time: 2025-11-07 13:23
 * @ Description:
 */
#include "cli.h"

static QCliObj cli;

int cli_init(void)
{
    int ret = qcli_init(&cli, kprintf);
    cli_title();
    return ret;
}

int cli_title(void)
{
    return qcli_title(&cli);
}


int cli_exec(char c)
{
    return qcli_exec(&cli, c);
}

int cli_echo(char *args)
{
    return qcli_echo(&cli, args);
}

int cli_add(const char *name, CliCallback cb, const char *usage)
{
    if(!name || !cb || !usage) {
        return -1;
    }

    QCliCmd *cmd = qmalloc(sizeof(QCliCmd));
    return qcli_add(&cli, cmd, name, cb, usage);
}

int cli_del(const char *name)
{
    if(!name) {
        return -1;
    }
    QCliCmd *cmd = qcli_find(&cli, name);
    if(!cmd) {
        return -1;
    }
    int retval = qcli_del(&cli, cmd);
    qfree(cmd);
    return retval;
}

int cli_help(CliArgsTable *table, size_t table_size)
{
    size_t l = 0;
    size_t sz = table_size / sizeof(CliArgsTable);

    for(size_t i = 0; i < sz; i++) {
        if(l < strlen(table[i].name)) {
            l = strlen(table[i].name);
        }
    }

    size_t n = sz;
    for(size_t i = 0; i < n; i++) {
        QSH(" -%-*s  %s\r\n", l, table[i].name, table[i].usage);
    }
    return 0;
}

